package com.leetcode.array.spiralmatrix.demo1;

/**
 * @Author Zan
 * @Create 2024/5/11 15:55
 * @ClassName: Test
 * @Description : https://leetcode.cn/problems/spiral-matrix-ii/description/  螺旋矩阵 II
 */
public class Test {
    public static void main(String[] args) {

    }
}

class Solution {
    public int[][] generateMatrix(int n) {
        int startX = 0, startY = 0;
        int offset = 1, count = 1;
        int[][] arr = new int[n][n];
        int i, j;
        int loop = 1; // 记录当前的圈数
        // 确定会循环几次
        while (loop <= n / 2) {
            for (j = startY; j < n - offset; j++) {
                arr[startX][j] = count++;
            }
            for (i = startX; i < n - offset; i++) {
                arr[i][j] = count++;
            }
            for ( ; j > startY; j--) {
                arr[i][j] = count++;
            }
            for ( ; i > startX; i--) {
                arr[i][j] = count++;
            }
            startX++;
            startY++;
            offset++;
            loop++;
        }
        if (n % 2 == 1) {
            arr[startX][startY] = count++;
        }
        return arr;
    }
}
